

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class AppContext {

    private static ApplicationContext springContext;

    public static void initContext() {
        springContext = new ClassPathXmlApplicationContext("spring-datasource-config.xml");
    }

    public static ApplicationContext getSpringContext() {
        while (springContext == null) {
        	try {
				Thread.sleep(1000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }
        return springContext;
   }
    
   public static Connection getConnection() {
	   Connection conn = null;
	   try {
		   DataSource  dataSource  = (DataSource)AppContext.getSpringContext().getBean("dataSource");
		   conn = dataSource.getConnection();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   return conn;
   }
}